//content.js
var utils = require('../../common/util');

var app = getApp();
var catalog;

var configOnload;
var config = {
  old: {
    chapter: 0,
    section: 0
  },
  new: {
    chapter: 0,
    section: 0
  }
};

Page({
  data: {
    chapterList: [],
    sectionList: [],
    chapterListScrollInto: 'c0',
    sectionListScrollInto: 's0',
    book: 'old'
  },

  listSections: function(count){
      var secs = [];
      for(var i = 0; i < count; i++){
        secs.push({
          title: '第' + utils.number2Chinese(i + 1) + '章' 
        });
      }
      return secs;
  },

  loadSections: function(evt){
    var index = evt.target.dataset.index;
    var b = this.data.book;
    
    var chapterIndex = config[b].chapter = index;

    var count = this.data.chapterList[chapterIndex].sectionsCount;
    var sectionList = this.listSections(count);
    var chapterList = this.data.chapterList;

    utils.selectOne(chapterIndex, chapterList);
    if(configOnload.book == b && configOnload.chapter == index){
      utils.selectOne(configOnload.section, sectionList);
      config[b].section = configOnload.section;
    }else{
      utils.selectOne(0, sectionList);
      config[b].section = 0;
    }

    this.setData({
      chapterList: chapterList,
      sectionList: sectionList,
      sectionListScrollTop: 0
    });
  },

  setCatalog: function(params){
      var book = params.book;
      var chapterIndex = config[book].chapter = params.chapter;
      var sectionIndex = config[book].section = params.section;

      var count = catalog[book][chapterIndex].sectionsCount;
      var chapterList = catalog[book];
      var sectionList = this.listSections(count);

      utils.selectOne(chapterIndex, chapterList);
      utils.selectOne(sectionIndex, sectionList);
      this.setData({
        book: book,
        chapterList: chapterList,
        sectionList: sectionList
      });
  },

  setScroll: function(ci, si){
    this.setData({
      chapterListScrollInto: 'c' + Math.max((ci - 4), 0),
      sectionListScrollInto: 's' + Math.max((si - 4), 0),
    });
  },

  onLoad: function (params) {
    configOnload = params;
    var self = this;

    app.getCatalog(function(data){
      catalog = data;
      self.setCatalog(params);
    });

    this.setScroll(params.chapter, params.section);
  },

  redirectToSection: function(b, ci, si, scroll){
    var scrollStr = scroll ? '&scroll=' + scroll : '' ;
    wx.redirectTo({
      url: '../content/content?book=' + b + '&chapter=' + ci + '&section=' + si + scrollStr
    });
  },

  gotoSection: function(evt){
    var b = this.data.book;
    var ci = config[b].chapter;
    var si = evt.target.dataset.index;

    this.redirectToSection(b, ci, si);
  },

  tabToOld: function(){
    var tmp = config['old'];
    
    this.setCatalog({
      book: 'old',
      chapter: tmp.chapter,
      section: tmp.section
    });
  },

  tabToNew: function(){
    var tmp = config['new'];

    this.setCatalog({
      book: 'new',
      chapter: tmp.chapter,
      section: tmp.section
    });
  },

  closeCatalog: function(){
    var b = configOnload.book;
    var ci = configOnload.chapter;
    var si = configOnload.section;
    var scroll = wx.getStorageSync('content-scroll');

    this.redirectToSection(b, ci, si, scroll);
  }
})
